import { useNotificationStore } from '../stores/notification';
import { getCurrentInstance } from 'vue';

/**
 * Hook to manage notifications.
 * It provides methods to show different types of notifications.
 * @param {object} [piniaInstance] - Optional pinia instance, useful outside of setup context.
 * @returns {object} - 包含 .success, .error 等方法的通知处理器。
 */
export function useNotification(piniaInstance) {
  const vm = getCurrentInstance();
  const pinia = piniaInstance || (vm ? vm.appContext.config.globalProperties.$pinia : undefined);
  const store = useNotificationStore(pinia);

  const show = (options) => {
    const id = store.add(options);

    if (options.duration !== 0 && options.duration > 0) {
      setTimeout(() => {
        store.remove(id);
      }, options.duration);
    }

    return {
      close: () => store.remove(id),
    };
  };

  return {
    show,
    success: (options) => show({ ...options, type: 'success' }),
    error: (options) => show({ ...options, type: 'error' }),
    warning: (options) => show({ ...options, type: 'warning' }),
    info: (options) => show({ ...options, type: 'info' }),
  };
} 